<?php
namespace app\appclass\model;
use app\BaseModel;
use think\facade\Db;



class ClassCate extends BaseModel
{
    //定义表名
    protected $name = 'longbing_card_appclass_class_cate';


    /**
     * @author chenniang
     * @DataTime: 2021-05-11 16:00
     * @功能说明:添加
     */
    public function dataAdd($data){

        $data['create_time'] = time();

        $res = $this->insert($data);

        return $res;

    }


    /**
     * @author chenniang
     * @DataTime: 2021-05-11 16:03
     * @功能说明:编辑
     */
    public function dataUpdate($dis,$data){

        $res = $this->where($dis)->update($data);

        return $res;

    }


    /**
     * @author chenniang
     * @DataTime: 2021-05-11 16:04
     * @功能说明:详情
     */
    public function dataInfo($dis){

        $data = $this->where($dis)->find();

        return !empty($data)?$data->toArray():[];

    }


    /**
     * @author chenniang
     * @DataTime: 2021-05-11 16:05
     * @功能说明:
     */
    public function dataList($dis,$page=10){

        $data = $this->where($dis)->where('status','>',-1)->order('id desc')->paginate($page)->toArray();

        return $data;

    }


    /**
     * @author chenniang
     * @DataTime: 2021-06-18 16:05
     * @功能说明:下架删除时检查排课 和课程有无使用改分类
     */
    public function check($id){

        $class_model = new ClassList();

        $date_model  = new ClassDate();

        $class_where[] = ['cate_id','=',$id];

        $class_where[] = ['status','>',-1];

        $class = $class_model->dataInfo($class_where);

        if(!empty($class)){

            $this->errorMsg('该分类下有课程');
        }

        $date_where[] = ['cate_id','=',$id];

        $date_where[] = ['status','>',-1];

        $date_where[] = ['start_time','>',time()];

        $date = $date_model->dataInfo($date_where);

        if(!empty($date)){

            $this->errorMsg('该分类下有排课');
        }

        return true;
    }





}